﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.ServiceModel;
using WcfAfvandingServiceLibrary;

namespace Pumpestation1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        ServiceHost selfHost;
        IAfvandingL afvanding;

        public MainWindow()
        {
            InitializeComponent();
            Server();
            Client();
        }

        public void Server()
        {
            selfHost = new ServiceHost(typeof(AfvandingL));

            try
            {
                selfHost.AddServiceEndpoint(typeof(IAfvandingL), new NetTcpBinding(), Constants.URI);
                // Starter serveren
                selfHost.Open();
                Console.ReadLine();
            }
            catch (CommunicationException ce)
            {
                MessageBox.Show("Fejl : " + ce);
                selfHost.Abort();
            }
        }

        public void Client()
        {
            ChannelFactory<IAfvandingL> cfAfvanding = new ChannelFactory<IAfvandingL>(new NetTcpBinding(), Constants.URI);
            afvanding = cfAfvanding.CreateChannel();
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (MessageBox.Show("Er du sikker på du ville lukke servicen?", "meddelelse", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                selfHost.Close();
            }
        }
    }
}
